These are called generalized type constraints They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here's an example: case class FooA(a:A) { // 'A' can be substituted with any type // getStringLength can only be used if this is a FooString def getStringLength(implicit evidence: A =:= String) = a. Length } The implicit argument evidence is supplied by the compiler, iff A is String You can think of it as a proof that A is String the argument itself isn't important, only knowing that it exists edit: well, technically it actually is important because it represents an implicit conversion from A to String which is what allows you to call a.
Length and not have the compiler yell at you Now I can use it like so: scala> Foo("blah"). GetStringLength res6: Int = 4 But if I tried use it with a Foo containing something other than a String : scala> Foo(123). GetStringLength :9: error: could not find implicit value for parameter evidence: =:=Int,String You can read that error as "could not find evidence that Int == String"... that's as it should be!
GetStringLength is imposing further restrictions on the type of A than what Foo in general requires; namely, you can only invoke getStringLength on a FooString This constraint is enforced at compile-time, which is cool! : and % work similarly, but with slight variations: A =:= B means A must be exactly B A SumInts method, which adds up a list of integers. You don't want to allow this method to be invoked on any old List just a ListInt However the List type constructor can't be so constrainted; you still want to be able to have lists of strings, foos, bars, and whatnots.
So by placing a generalized type constraint on sumInts you can ensure that just that method has an additional constraint that it can only be used on a ListInt Essentially you're writing special-case code for certain kinds of lists.
These are called generalized type constraints. They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here's an example: case class FooA(a:A) { // 'A' can be substituted with any type // getStringLength can only be used if this is a FooString def getStringLength(implicit evidence: A =:= String) = a.
Length } The implicit argument evidence is supplied by the compiler, iff A is String. You can think of it as a proof that A is String--the argument itself isn't important, only knowing that it exists. Edit: well, technically it actually is important because it represents an implicit conversion from A to String, which is what allows you to call a.
Length and not have the compiler yell at you Now I can use it like so: scala> Foo("blah"). GetStringLength res6: Int = 4 But if I tried use it with a Foo containing something other than a String: scala> Foo(123). GetStringLength :9: error: could not find implicit value for parameter evidence: =:=Int,String You can read that error as "could not find evidence that Int == String"... that's as it should be!
GetStringLength is imposing further restrictions on the type of A than what Foo in general requires; namely, you can only invoke getStringLength on a FooString. This constraint is enforced at compile-time, which is cool! ADDENDUM To answer your follow-up question, admittedly the example I gave is pretty contrived and not obviously useful.
But imagine using it to define something like a List. SumInts method, which adds up a list of integers. You don't want to allow this method to be invoked on any old List, just a ListInt.
However the List type constructor can't be so constrainted; you still want to be able to have lists of strings, foos, bars, and whatnots. So by placing a generalized type constraint on sumInts, you can ensure that just that method has an additional constraint that it can only be used on a ListInt. Essentially you're writing special-case code for certain kinds of lists.
2 Well, ok, but there's also methods by the same names on Manifest, which you didn't mention. – Daniel C. Sobral Aug 6 '10 at 23:25 1 The methods on Manifest are :> only... since OP mentioned exactly the 3 varieties of generalized type constraints, I'm assuming that's what he was interested in.
– pelotom Aug 7 '10 at 0:00 2 The error message the user will get when using the getStringLength function will not be very descriptive though... It reminds me C++'s errors. – Elazar Leibovich Aug 7 '10 at 22:13 7 @IttayD: it's pretty clever... class =:=From, To extends From => To, which means that an implicit value of type From =:= To is actually an implicit conversion from From to To. So by accepting an implicit parameter of type A =:= String you're saying that A can be implicitly converted to String.
If you changed the order and made the implicit argument be of type String =:= A, it wouldn't work, because this would be an implicit conversion from String to A. – pelotom Aug 8 '10 at 9:34 3 Do those three-character symbols have names? My problem with Scala's symbol soup is that they're hard to talk about verbally, and it's practically impossible to use Google or any other search engine to find discussions and examples of their usage.
– Gigatron Mar 26 at 19:03.
Not a complete answer (others have already answered this), I just wanted to note the following, which maybe helps to understand the syntax better: The way you normally use these "operators", as for example in pelotom's example: def getStringLength(implicit evidence: A =:= String) makes use of Scala's alternative infix syntax for type operators. So, A =:= String is the same as =:=A, String (and =:= is just a class or trait with a fancy-looking name). Note that this syntax also works with "regular" classes, for example you can write: val a: Tuple2Int, String = (1, "one") like this: val a: Int Tuple2 String = (1, "one") It's similar to the two syntaxes for method calls, the "normal" with .
And () and the operator syntax.
It depends on where they are being used. Most often, when used while declaring types of implicit parameters, they are classes. They can be objects too in rare instances.
Finally, they can be operators on Manifest objects. They are defined inside scala. Predef in the first two cases, though not particularly well documented.
They are meant to provide a way to test the relationship between the classes, just like Either through Right. */ def joinRightA1 >: A, B1 >: B, C(implicit ev: B1 Left(a) case Right(b) => be } /** * Joins an Either through Left. */ def joinLeftA1 >: A, B1 >: B, C(implicit ev: A1 a case Right(b) => Right(b) } On Option, you have: def orNullA1 >: A(implicit ev: Null.
" applies to a great many things. – Mike Miller Aug 6 '10 at 20:50 "They are meant to provide a way to test the relationship between the classes" I'd like to be able to make that determination for myself. – Jeff Aug 7 '10 at 0:01.
Read the other answers to understand what these constructs are. Here is when you should use them. You use them when you need to constrain a type for specific methods only.
Here is an example. Suppose you want to define a homogeneous Pair, like this: class PairT(val first: T, val second: T) Now you want to add a method smaller, like this: def smaller = if (first With a type constraint, you can still define the smaller method: def smaller(implicit ev: T You can use orNull on an OptionString, and you can form an OptionInt and use it, as long as you don't call orNull on it. If you try Some(42).
OrNull, you get the charming message error: Cannot prove that Null.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.